package com.core.convenientCode;

import com.common.utils.StringUtils;
import com.core.convenientCode.common.GenUtils;
import com.core.springSecurity.common.SecurityUtils;
import com.dao.primary.sys.SysGenTableMapper;
import com.model.pojo.sys.SysGenTable;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: zql
 * @Description: 数据库表记录，用于自动化生成代码
 * @Data: 9:43 2021/6/1
 */
@Service
public class SysGenTableService {

    @Resource
    SysGenTableMapper sysGenTableMapper;

    @Resource
    SysGenTableColumnService sysGenTableColumnService;

    /**
     * @Author: zql
     * @Description: 获取当前数据库中未导入的表
     * @param
     * @Data: 10:33 2021/6/1
     */
    public List<SysGenTable> getNotInsertGenTableList(){
       return sysGenTableMapper.selectNotInsertDBList();
    }

    /**
     * @Author: zql
     * @Description: 同步更新库中新增的表
     * @param
     * @Data: 10:03 2021/6/1
     */
    public List<String> syncGenTable(){
        //查询已导入的表
        List<SysGenTable> sysGenTableList= getList();
        List<String> tableNameList= sysGenTableList.stream().map(SysGenTable::getTableName).collect(Collectors.toList());
        //查询当前所有表
        List<SysGenTable> sysGenTableListDB =sysGenTableMapper.selectDBList();
        List<String> tableNameListDB= sysGenTableListDB.stream().map(SysGenTable::getTableName).collect(Collectors.toList());

        String operName = SecurityUtils.getUsername();
        sysGenTableListDB.forEach(table->{
            //初始化一部分字段
            GenUtils.initTable(table,operName);
            table.setCreator(SecurityUtils.getUsername());
            table.setModifier(SecurityUtils.getUsername());
           if( tableNameList.contains(table.getTableName())){
                //存在就更新
               sysGenTableMapper.updateByTableName(table);
           }
           else{
                //不存在就增加
               sysGenTableMapper.insertSelective(table);
           }
        });
        //删除已删除的
        List<SysGenTable> deleteTableList = sysGenTableList.stream().filter(table->!tableNameListDB.contains(table.getTableName())).collect(Collectors.toList());
        if(StringUtils.isNotEmpty(deleteTableList)){
            for(SysGenTable sysGenTable: deleteTableList){
                sysGenTableColumnService.deleteByTableId(sysGenTable.getSysGenTableId());
                sysGenTableMapper.deleteByPrimaryKey(sysGenTable.getSysGenTableId());
            }
        }
        return tableNameListDB;
    }


    /**
     * @Author: zql
     * @Description: 查询当前导入的表
     * @param
     * @Data: 10:15 2021/6/1
     */
    public List<SysGenTable> getList(){
       return sysGenTableMapper.selectList();
    }

    /**
     * @Author: zql
     * @Description:根据表名查询表信息
     * @param tableName
     * @Data: 9:28 2021/6/2
     */
    public SysGenTable getSysGenTable(String tableName){
        return sysGenTableMapper.selectByTableName(tableName);
    }

    //更新并删除不存在的表
}
